package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserPowerChild;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserPowerChildDaoImpl extends TempDataObjectDao<UserPowerChild> {

    public UserPowerChild getUserPowerChild(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        UserPowerChild data = null;
        String sql = "select * from t_u_power_child where userId=? limit 1";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                data = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return data;
    }

    @Override
    protected boolean addData(UserPowerChild data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_power_child(userId, childUid, childTalent, childSex, childOccupation, childName, childHeadIcon, " +
                "childAdultTime, motherBeautyId, motherThenIntimacy, childOccupationId, childEarnSpeed, isTwins)values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getChildUid());
            call.setInt(i++, data.getChildTalent());
            call.setInt(i++, data.getChildSex());
            call.setInt(i++, data.getChildOccupation());
            call.setString(i++, data.getChildName());
            call.setInt(i++, data.getChildHeadIcon());
            call.setLong(i++, data.getChildAdultTime());
            call.setInt(i++,data.getMotherBeautyId());
            call.setInt(i++,data.getMotherThenIntimacy());
            call.setInt(i++, data.getChildOccupationId());
            call.setString(i++, data.getChildEarnSpeed().toString());
            call.setBoolean(i++, data.getIsTwins());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserPowerChild data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_power_child SET childUid=?, childTalent=?, childSex=?, childOccupation=?, childName=?, childHeadIcon=?, childAdultTime=?, " +
                "motherBeautyId=?, motherThenIntimacy=?, childOccupationId=?, childEarnSpeed=?,isTwins=? where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setString(i++, data.getChildUid());
            call.setInt(i++, data.getChildTalent());
            call.setInt(i++, data.getChildSex());
            call.setInt(i++, data.getChildOccupation());
            call.setString(i++, data.getChildName());
            call.setInt(i++, data.getChildHeadIcon());
            call.setLong(i++, data.getChildAdultTime());
            call.setInt(i++,data.getMotherBeautyId());
            call.setInt(i++,data.getMotherThenIntimacy());
            call.setInt(i++, data.getChildOccupationId());
            call.setString(i++, data.getChildEarnSpeed().toString());
            call.setBoolean(i++, data.getIsTwins());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserPowerChild parseData(ResultSet rs) throws SQLException {
        UserPowerChild data = new UserPowerChild();
        data.setUserId(rs.getLong("userId"));
        data.setChildUid(rs.getString("childUid"));
        data.setChildTalent(rs.getInt("childTalent"));
        data.setChildSex(rs.getInt("childSex"));
        data.setChildOccupation(rs.getInt("childOccupation"));
        data.setChildName(rs.getString("childName"));
        data.setChildHeadIcon(rs.getInt("childHeadIcon"));
        data.setChildAdultTime(rs.getInt("childAdultTime"));
        data.setMotherBeautyId(rs.getInt("motherBeautyId"));
        data.setMotherThenIntimacy(rs.getInt("motherThenIntimacy"));
        data.setChildOccupationId(rs.getInt("childOccupationId"));
        String value = rs.getString("childEarnSpeed");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setChildEarnSpeed(new BigInteger(value));
        }
        data.setIsTwins(rs.getBoolean("isTwins"));
        return data;
    }
}
